#include "date.h"

Date::Date()
{
    _year = 2024;
    _month = 9;
    _day = 15;
}

Date::Date(int year, int month, int day)
{
    assert(checkDate(year, month, day));
    _year = year;
    _month = month;
    _day = day;
}

// 成员变量是内置类型,其实不需要写拷贝构造函数,系统会自动生成一个默认的拷贝构造函数
Date::Date(const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
}

Date& Date::operator=(Date& d)
{
    assert(checkDate(d._year, d._month, d._day));
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
}

Date Date::operator+(int days) const
{
    Date temp = *this;
    temp += days;
    return temp;
}

Date& Date::operator+=(int days)
{
    _day += days;
    while(_day > getMonthDays(_year, _month))
    {
        _day -= getMonthDays(_year, _month);
        ++_month;
        if(_month == 13)
        {
            _month = 1;
            _year++;
        }
    }
    while(_day < 1)
    {
        --_month;
        if(_month == 0)
        {
            _month = 12;
            _year--;
        }
        _day += getMonthDays(_year, _month);
    }
    return *this;
}

Date& Date::operator-=(int days)
{
    return (*this)+=-days;
}

Date Date::operator-(int days) const
{
    return (*this) + (-days) ;
    // Date temp = *this;
    // temp._day -= days;
    // return temp; 
}

int Date::operator-(const Date& d) const
{
    int days = 0;
    int symb = (*this) > d? 1:-1;
    Date temp = *this;
    while(temp != d)
    {
        temp -= symb;
        days++;
    }
    return days * symb;
}

Date& Date::operator++()
{
    return (*this)+=1;
}
Date& Date::operator--()
{
    return (*this)-=1;
}

Date Date::operator++(int)
{
    Date tmp = *this;
    (*this)+=1;
    return tmp;
}

Date Date::operator--(int)
{
    Date tmp = *this;
    (*this)-=1;
    return tmp;
}

bool Date::operator==(const Date& d) const
{
    return _year==d._year &&
        _month==d._month &&
        _day==d._day;
}

bool Date::operator!=(const Date& d) const
{
    return !((*this) == d);
}

bool Date::operator>(const Date& d) const
{
    // if(this->_year > d._year)
    //     return true;
    // if(this->_year == d._year && this->_month > d._month)
    //     return true;
    // if(this->_year == d._year && this->_month == d._month && this->_day > d._day)
    //     return true;
    // return false;
    if(this->_year != d._year)
        return this->_year > d._year;
    if(this->_month != d._month)
        return this->_month > d._month;
    if(this->_day != d._day)
        return this->_day > d._day;
    return false;
}

bool Date::operator<(const Date& d) const
{
    // return !((*this)>d) && ((*this) != d) ;
    return !((*this) >= d) ;
}

bool Date::operator>=(const Date& d) const
{
    return (*this)>d || (*this)==d ;
}

bool Date::operator<=(const Date& d) const
{
    return !((*this)>d) ;
}

//这样写只能把cout放在右边,不符合常规使用习惯
std::ostream& Date::operator<<(std::ostream& out) const
{
    return out << _year << "年" << _month << "月" << _day << "日" ;
}

//定义全局的<<操作符重载函数
std::ostream& operator<<(std::ostream& out, const Date& date)
{
    return out << date._year << "年" << date._month << "月" << date._day << "日";
}

std::istream& operator>>(std::istream& in, Date& date)
{
    std::cout << "请依次输入年月日:>" ;
    in >> date._year >> date._month >> date._day;
    assert(date.checkDate(date._year, date._month, date._day));
    return in;
}